package com.sz.app.oss.api.trade;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.trade.ord.entity.TrdOrderFileRel;
import com.sz.biz.trade.ord.service.TrdOrderFileRelService;
import com.sz.biz.trade.ord.dto.TrdOrderFileRelDto;
import com.sz.biz.logistics.ord.entity.OrderStatus;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Package: com.sz.app.oss.api.trade<br>
 * Function: 贸易订单附件controller
 * Author: penghui.zheng<br>
 * Date: 2017-11-16 10:35:00.
 */
@RestController
@RequestMapping("/api/v1/trd/order/files")
@Api(description = " ", tags = "043-001、贸易-订单附件管理")
public class TrdOrderFileController extends BaseController {
    @Autowired
    private TrdOrderFileRelService trdOrderFileRelService;



    /**
     * 异步校验某个订单下，附件名称的唯一性
     */
    @ApiOperation(value = "校验某个订单下，附件名称的唯一性", notes = "校验附件名称的唯一性,false表示存在，true表示不存在",position = 2)
    @ApiImplicitParams({

            @ApiImplicitParam(name = "orderNo", value = "订单编号,新建订单时不需要", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "fileName", value = "附件名称", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/files/name/check", method = {RequestMethod.GET})
    public ResultDto checkName(@RequestParam String orderNo, @RequestParam String fileName) {
        Boolean result = trdOrderFileRelService.checkUniqueName(orderNo, fileName);
        ResultDto resultDto = new ResultDto();
        resultDto.setData(result);
        return resultDto;
    }

    /**
     * 新增订单附件
     */
    @ApiOperation(value = "新增订单的附件", notes = "新增订单附件<br>权限：" + Permissions.BIZ_MGT_TRD_ORDER)
    @RequestMapping(value = "/{orderNo}/orderFiles", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_TRD_ORDER)
    public ResultDto addTrdOrderFile(@PathVariable String orderNo, @RequestBody @ApiParam(value = "要保存的数据", required = true) @Validated TrdOrderFileRelDto trdOrderFileRelDto) {
        trdOrderFileRelDto.setOrderNo(orderNo);
        TrdOrderFileRel trdOrderFileRel = trdOrderFileRelService.addOrderFileDto(trdOrderFileRelDto);
        ResultDto result = getSaveSuccessDto(MessagesCodes.MODULE_ORDER_FILE);
        result.setData(trdOrderFileRel);
        return result;
    }

    /**
     * 删除订单附件
     *
     * @return
     */
    @ApiOperation(value = "删除订单的附件", notes = "删除订单附件<br>权限：" + Permissions.BIZ_MGT_TRD_ORDER)
    @RequestMapping(value = "/{orderNo}/{orderFileId}/delete", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_TRD_ORDER)
    public ResultDto deleteOrderFile(@PathVariable String orderNo, @PathVariable Integer orderFileId) {
        trdOrderFileRelService.deleteTrdOrderFile(orderNo, orderFileId);
        return getDeleteSuccessDto(MessagesCodes.MODULE_ORDER_FILE);
    }


    /**
     * 分页获取某个订单的附件
     */

    @ApiOperation(value = "分页获取某个订单下的附件列表", notes = "分页获取某个订单下的附件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query", defaultValue
                    = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数,max<2000", dataType = "int", paramType = "query",
                    defaultValue = "50"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue =
                    "true"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "orderStatus", value = "订单的状态", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResultDto find(@RequestParam(required = false) String q,
                          @RequestParam(required = false, defaultValue = "1") int pageIndex,
                          @RequestParam(required = false, defaultValue = "50") int pageSize,
                          @RequestParam String orderNo,
                          @RequestParam(required = false) OrderStatus orderStatus,
                          @RequestParam(required = false) String sort,
                          @RequestParam(required = false, defaultValue = "true") Boolean asc
    ) {
        q = StringUtils.replaceSqlPattern(q);
        QueryParams qp = new QueryParams(pageSize, pageIndex, q, sort, asc);
        qp.put("orderNo", orderNo.trim());

        if (!ObjectUtils.isEmpty(orderStatus)) {
            qp.put("orderStatus", orderStatus);
        }
        return trdOrderFileRelService.findFileDtoPages(qp);
    }

}
